<?php
//error_reporting(0);
define('IN_ADAGPS', TRUE);
define('ROOTURL', 'index.php');
$sTplRootUrl = ROOTURL;
global $arrConfig,$generalTitle,$lang,$tbl_prefix,$modulecall,$ajax,$strMainMenu,$objXajax,$objDbSelect;
$tbl_prefix = TBL_PREFIX;
include_once('adagps.com/cfg.php');
include_once(CORE_PATH.'includes/constant_common.php');// chứa các hằng số sử dụng chung cho cả Vi và EN v co thay doi
include_once(CORE_PATH.'includes/functions.php');
include_once(CORE_PATH.'includes/functions_xajax.php');
include_once(CORE_PATH.'database/cls_table.php');
include_once(CORE_PATH.'database/cls_table_search.php');
include_once(CORE_PATH.'database/commonDB.php');
$objXajax->registerFunction('ajxLogout');
$objXajax->registerFunction('ajxLoadContent');
$objXajax->registerFunction('frmindex_save');
$objXajax->registerFunction('frmchangepass_save');
//$objXajax->registerFunction('SetConfigure');
$objXajax->registerFunction('loadregist');
$objXajax->registerFunction('loadAlertRegist');
$objXajax->registerFunction('updateRegist');
$objXajax->registerFunction('deleteRegist');

$tbl_prefix = TBL_PREFIX;
$sys_active = ACTIVE;
$sys_deactive = DEACTIVE;
$tbl_postfix = '';
$hotel_id='';
$user_id = '';
$lang = 'vi';
$path = 'customer';

if(!ConnectDB())
{
    if( $_REQUEST['xajax']==null){
        echo $objDbSelect->ErrorMsg(), '<br>';
    }       
    if($arrConfig['debug'] === true)
    {
        Die(CONNECT_DB_ERR);
    }else{
        $_POST['xajax']='ajxLogout';
    }
    Die(CONNECT_DB_ERR);
}

if(IsLogin()){
    if($_SESSION['current']['tbl_postfix']!=''){
        $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    }
     if($_SESSION['current']['hotel_id']!=''){
        $hotel_id = $_SESSION['current']['hotel_id'];
    }
    if($_SESSION['current']['user_id']!=''){
        $user_id = $_SESSION['current']['user_id'];
    }
    if(isset($_SESSION['current']['lang'])){
        $lang = $_SESSION['current']['lang'];
    }
    
    $sModule = GetModule();
    if($_GET["m"]=='logout'){// trường hợp bấm nút logout
        logout();
        CloseDB();
    }else{
    $tplXajaxScript = ''; 
    $powertype = $_SESSION['current']['power_type_id']; // trung voi bien phia duoi
    if($_SESSION['current']['flg_cus']!=1) {
        $path = 'management';
      include(ADAGPS_PATH.'module_management.php');   
    }else{
        include(ADAGPS_PATH.'module_customer.php'); 
    }
    
    
    if($_REQUEST['xajax']==null){      
    $power_type_id = $_SESSION['current']['power_type_id'];
    $sql = sqlForMainmenu();
    $arMenu = $objDbSelect->GetArray($sql);
    //echo   $sql; die();
    $last = 0;
    for($i = 0; $i < count($arMenu); $i++) 
    {
        
        if($i == 0)
            $arMenu[$i]['row'] = ' ui-corner-left';
        else
            $arMenu[$i]['row'] = '';
        $arMenu[$i]['index'] = $i;
        $last = $i;
    }
    if($last > 0)
        $arMenu[$last]['row'] = ' ui-corner-right';

    $sIndexFile = TEMPL_PATH.$lang.'/index.html';
    $objTemplate->LoadTemplate($sIndexFile, 'UTF-8');
    $objTemplate->MergeBlock('menu', $arMenu);
    $objTemplate->MergeBlock('menu2', $arMenu);
    
    }else{
        $fnName   = $_REQUEST['xajax'];
        $arrName  = explode('_',$fnName);
        $nameFile = str_replace('frm','main_',$arrName[0]) ;
    }
    $objXajax->processRequests();
    $tplXajaxScript = $tplXajaxScript.$objXajax->getJavascript('adagps.com/scripts/xajax');
    $objTemplate->Show(); 
    CloseDB(); 
    }

}else{
    $sModule = GetModule(true);
    if($_GET["m"]!="")
    {
        $tplXajaxScript = '';

        $sIncFile = MODULE_PATH.'login/'.$_GET["m"].'.php';
        $strnamedisplay = getNameToDisplay();
        if(file_exists($sIncFile))
        {
            include_once($sIncFile);
        }
        $objXajax->processRequests();
        $tplXajaxScript = $tplXajaxScript.$objXajax->getJavascript('adagps.com/scripts/xajax');
        CloseDB();
        // Show page
        $objTemplate->Show();


    }else
    {
        $tplXajaxScript = '';

        //$sIncFile = MODULE_PATH.'login/main_'.$sModule.'_2.php';
        $sIncFile = MODULE_PATH.'login/login.php';
        $strnamedisplay = getNameToDisplay();
        if(file_exists($sIncFile))
        {
            include_once($sIncFile);
        }
        $objXajax->processRequests();
        $tplXajaxScript = $tplXajaxScript.$objXajax->getJavascript('adagps.com/scripts/xajax');
        CloseDB();
        // Show page
        $objTemplate->Show();
    }

}

function sqlForMainmenu(){
    global  $sys_active;
    $user_id =  $_SESSION['current']['user_id'];
    $power_type_id = $_SESSION['current']['power_type_id'];
    $flg_cus = $_SESSION['current']['flg_cus'];
    $result =''; 
    if($flg_cus==0){
      $result =  " select * from tbl_main_menu WHERE id in (select main_menu_id from tbl_mainmenu_powertype 
    where power_type_id ='$power_type_id') AND active = $sys_active order by ordershow "; 
    }else{
       $temp = "select * from tbl_main_menu_cus "  ;   // đang làm
       $where ='';
       $sertype ='0'; 
       $result =  $temp.' order by ordershow ' ;
    }
    return $result;
}

function ajxLogout($arrParams=null)
{
    $objResponse = new xajaxResponse();
    session_destroy();
    $objResponse->addRedirect('index.php?m=login');
    return $objResponse->getXML();
}

function logout()
{
    global $objDbSelect,$sessionID;
    session_destroy();
  
    Redirect('index.php?m=login'); 
    exit;  
}

function getModule($login=false){ 
    global  $arrModuleLogin;  
    $module = 'login';    
    if(isset($_GET['m'])){
      $temp = $_GET['m'];
      if($login){          
          if(in_array($temp,$arrModuleLogin)){
            $module = $temp;  
          }
      }else{
        $module = $temp; 
      }
    }
    return $module;
}

function ajxLoadContent($main_menu_id)
{
    global $objDbSelect,$lang,$main_menu,$user_id,$hotel_id;
    $active= ACTIVE;
    $objResponse = new xajaxResponse();
    $power_type_id = $_SESSION['current']['power_type_id'];
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $user_id = $_SESSION['current']['user_id'];

  $addCondition = '';
  $sql = '';
  if($power_type_id== POWER_CUSTOMER||$power_type_id== POWER_SUB_CUSTOMER){

      
      $condition ='';
      if($power_type_id==POWER_SUB_CUSTOMER){
         $condition = " and id in (select menu_cus_id from tbl_menu_for_subuser where service_id = $hotel_id )" ;
      } 
      $sql = " select distinct *,name_$lang as menuname from tbl_menu_cus where 
               main_menu_id = $main_menu_id  and active = $active $condition
               order by ordershow";
               
                  
  }else{
     $sql = "select distinct *,name_$lang as menuname from tbl_menu where main_menu_id = $main_menu_id and active = $active
     and  id in  (select menu_id from tbl_menu_powertype where power_id= '$power_type_id' and active =$active)
     order by ordershow";   
  }
  
    $arr = $objDbSelect->GetArray($sql);   
    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('templates/'.$lang.'/page_main.html');
    $oTbs->MergeBlock('blk_data', $arr);
    $oTbs->Show(TBS_NOTHING);
    $main_tabs = "main_tabs_$main_menu_id";
    $objResponse->addAssign($main_tabs, 'innerHTML', $oTbs->Source);
    $objResponse->addScript("createTab('$main_tabs');");
    $objResponse->addScript("resizePage()"); 
    
    if(count($arr)<1){
      $objResponse->addAlert("Bạn không có quyền với các chức năng trong mục này");   
    }  
    
           
    return $objResponse->getXML();
}

function frmindex_save($arrparams)
{
    global $objDbSelect;
     $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $name = addslashes($arrparams['txtName']);
    $mail = $arrparams['txtMail'];
    $phone = $arrparams['txtPhone'];
    $title = addslashes($arrparams['txtSubj']);
    $content =addslashes($arrparams['txtMesg']) ;
    $sql = " insert into tbl_feedback(name,mail,phone,title,content,date_created,user_created)
     value('$name','$mail','$phone','$title','$content',now(),'$user_id')";   
     $objDbSelect->Execute($sql);
    $objResponse->addScript('changeWaiting(1);'); 
    $objResponse->addScript("$('#frmindex_feedback').dialog('close');"); 
    return $objResponse->getXML();
}
function frmchangepass_save($arrparams)
{
    global $objDbSelect;
     $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $oldpass = EncryptPass($arrparams['txtOldPass']);
    $newpass = $arrparams['txtNewPass'];
    $newpassconfirm = $arrparams['txtNewPassConfirm'];
    if(strlen($newpass)<6){
      $objResponse->addScript("$('#frmchangepass_divalert').text('Vui lòng nhập mật khẩu mới trên 5 kí tự');");  
    }elseif($newpass!=$newpassconfirm){
      $objResponse->addScript("$('#frmchangepass_divalert').text('Mật khẩu mới và mật khẩu xác nhận không trùng');");  
    }else{
        $sqlconfirm = "select count(id) as rowcount from tbl_user where pass = '$oldpass' and id = '$user_id'";
        $arr = $objDbSelect->GetArray($sqlconfirm);
        if($arr[0]['rowcount']>0&&is_array($arr)){
            $newpass = EncryptPass($newpass);
           $sqlupdate = "update  tbl_user SET pass = '$newpass' WHERE id = '$user_id'";
           $objDbSelect->Execute($sqlupdate);
           $objResponse->addScript("$('#frmchangepass_dialog').dialog('close');");   
        }else{
           $objResponse->addScript("$('#frmchangepass_divalert').text('Mật khẩu cũ sai, vui lòng nhập lại!');");
        }     
    
    }
  $objResponse->addScript('changeWaiting(1);');
  return $objResponse->getXML();
}


function loadregist(){
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = "select * from tbl_cus_regist where service_id = '$hotel_id' and (
    datediff(date_register,now())=0 or status_process = 0) ";
    //echo $sql; die();
    $arr = $objDbSelect->GetArray($sql);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/cus_regist_grid.html');
    $oTbs->MergeBlock('blk_data', $arr);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmcusregist_dialog_grid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmregist_tbl');");
    $objResponse->addScript("$('#frmcusregist_dialog').dialog('open')"); 
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse -> getXML(); 
}


function loadAlertRegist(){
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();
    session_start(); 
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = "select status_process from tbl_cus_regist where service_id = '$hotel_id' and (
    datediff(date_create,now())=0 or status_process = 0) group by status_process order by status_process"; 
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    if(is_array($arr) && $count>0){
        $status_process = $arr[0]['status_process'];
        if($status_process==0){
           $objResponse->addScript("$('#registdiv').removeClass('divAlert_0').addClass('divAlert_1');"); 
        }else{
           $objResponse->addScript("$('#registdiv').removeClass('divAlert_1').addClass('divAlert_0');"); 
        }
       $objResponse->addScript("$('#registdiv').show();"); 
    }else{
      $objResponse->addScript("$('#registdiv').hide();");   
    }    
    return $objResponse -> getXML(); 
}
function updateRegist($id){
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = "update  tbl_cus_regist set status_process =1 where service_id = '$hotel_id' and id = '$id'"; 
    $arr = $objDbSelect->Execute($sql);
    $objResponse->addScript("$('#cusregistupdate_$id').remove();");  
    return $objResponse -> getXML(); 
}
function deleteRegist($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = "delete from tbl_cus_regist  where service_id = '$hotel_id' and id = '$id'"; 
    $arr = $objDbSelect->Execute($sql);
    $objResponse->addScript("$('#tr_regist$id').remove();");  
    return $objResponse -> getXML(); 
}


 